package com.ruoyi.jpbi.controller.organization;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.jpbi.domain.konggu.Companys;
import com.ruoyi.jpbi.domain.konggu.Tree;
import com.ruoyi.jpbi.service.CompanysService;
import com.ruoyi.jpbi.service.DepartmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/orgs")
public class OrgsController extends BaseController {


    @Autowired
    private CompanysService companysService;

    @Autowired
    private DepartmentsService departmentsService;

    private String prefix = "jpbi/organization/orgs";


    /**
     * 跳转分页页面
     */
    @GetMapping()
    public String user() {
        return prefix + "/orgs";
    }


    /**
     * 获取分页数据以及数据总量
     */
    @PostMapping("/list")
    @ResponseBody
    public void list() {
        System.out.println("ssss");
    }



    @GetMapping("findAllTree")
    @ResponseBody
    public Tree findAllTree(Companys companys,String type) {
        List<String> AllID = new ArrayList<String>();
        String CompID = null;
        if (companys.getCompID() != null) {
            CompID = companys.getCompID().intValue() - 10000 + "";
            AllID = this.GetCompanyandSubsidiaryID(CompID, "Comp");
        }else{
            CompID = "33";
            AllID = this.GetCompanyandSubsidiaryID("33", "Comp");
        }
        Tree t = departmentsService.findAllTree(AllID, CompID,type);
        return t;
    }

    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(String type) {
        System.out.println(type);
        List<Ztree> ztrees;
        ztrees = this.initZtree(companysService.findAllTree(type));
        return ztrees;
    }

    /**
     * 对象转部门树
     *
     * @param deptList 部门列表
     * @return 树结构列表
     */

    public List<Ztree> initZtree(List<Map<String, Object>> deptList) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        Ztree ztree;
        int i = 0;
        for (Map dept : deptList) {
            ztree = new Ztree();
            ztree.setId(Long.parseLong(dept.get("id").toString()));
            ztree.setpId(Long.parseLong(dept.get("pid").toString()));
            ztree.setName(dept.get("name").toString());
            ztree.setTitle(dept.get("name").toString());
            ztree.setOpen(false);
            ztree.setChecked(false); //默认全部展开
            ztrees.add(ztree);
        }
        return ztrees;
    }


    /**
     * 根据公司ID取得包括子公司下所有的ID,根据部门ID取得包括子部门下的所有ID
     *
     * @param CompID 公司祖ID,部门祖ID
     * @param type   ID类型 Comp=公司  depart=部门
     * @return 所有公司ID的List, 所有部门ID的List
     */
    public List<String> GetCompanyandSubsidiaryID(String CompID, String type) {
        List<String> SubsidiaryList = new ArrayList<String>();//用于取得所有子公司ID,子部门ID
        List<String> AllId = new ArrayList<String>();//用于记录所有ID
        AllId.add(CompID);
        SubsidiaryList.add(CompID);
        do {
            if (type.equals("Comp")) {
                SubsidiaryList = companysService.findSubsidiaryID(SubsidiaryList);//获取所有子公司ID
            } else {
                SubsidiaryList = departmentsService.findSubdepartmentID(SubsidiaryList);//获取所有子部门ID
            }
            for (String id : SubsidiaryList) {
                AllId.add(id);
            }
        } while (SubsidiaryList.size() != 0);
        return AllId;
    }

}
